package com.chis.gencode;


import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

public class TestCode {

    /**
     * 生成文件
     *
     * @param msg
     * @param path
     * @return
     */

    public static File generateFile(String msg, String path) {
        File file = new File(path);
        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return file;
    }


    /**
     * 生成字节
     *
     * @param msg
     * @return
     */

    public static byte[] generate(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }


    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */

    public static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }
        Code128Bean bean = new Code128Bean();
        // 精细度
        final int dpi = 150;
        // module宽度
        //final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
        // 配置对象
        bean.setModuleWidth(0.25);
        bean.setBarHeight(8);

//        bean.doQuietZone(false);
        String format = "image/png";

        try {
            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);
            // 生成条形码
            bean.generateBarcode(canvas, msg);
            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public static void main(String[] args) {
        File f = new File("D:\\codemsg\\msgs");
        if(!f.exists()){
            f.mkdir();
        }
        File file1 = new File("D:\\codemsg\\code.txt");
        FileReader m = null;
        try {
            m = new FileReader(file1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(m);
        while (true) {
            String nextline = null;
            try {
                nextline = reader.readLine();
                if (nextline != null && nextline.length() == 20) {
                    generateFile(nextline, "D:\\codemsg\\msgs\\"+nextline+".png");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (nextline == null) break;
        }

//        String msg = "81002351235123051293,81002351235122641554,81002351235121614442,81002351235123327156,81002351235128813030,81002351235121886019,81002351235124492082,81002351235130524250,81002351235124766437,81002351235125932804,81002351235133136274,81002351235126206123,81002351235130256518,81002351235137451241,81002351235127377779,81002351235131690002,81002351235141775148,81002351235127646420,81002351235138899412,81002351235152285678,81002351235129083285,81002351235145088428,81002351235153721694,81002351235134570296,81002351235149400840,81002351235156600393,81002351235136016288,81002351235150844227,81002351235159486313,81002351235140336244,81002351235155163406,81002351235161254990,81002351235146521780,81002351235158046092,81002351235162693871,81002351235147969991,81002351235159816110,81002351235165240741,81002351235162363769,81002351235160920666,81002351235168124585,81002351235163802517,81002351235169563878,81002351235173880527,81002351235164134210,81002351235181084366,81002351235176763351,81002351235166689803,81002351235183961278,81002351235211231756,81002351235171001543,81002351235196532372,81002351235216999260,81002351235172446376,81002351235199412991,81002351235221317381,81002351235175328618,81002351235209792972,81002351235224192645,81002351235178203075,81002351235225633346,81002351235228516289,81002351235182522827,81002351235227070837,81002351235798578941,81002351235184800639,81002351235800014307,81002351236281141297,81002351235185407370,81002351235801452138,81002351236285464097,81002351235186842165,81002351236286905323,81002351236291221976,81002351235197975233,81002351236288341650,81002351236295547680,81002351235200850538,81002351236294103779,81002351261189220773,81002351235202293834,81002351236298424002,81002351267532747064,81002351235212678507,81002351267501409430,81002351267535627476,81002351235214119609,81002351267504281596,81002351267538884394,81002351235215555127,81002351267531684022,81002351267541760384,81002351235218435448,81002351267536002089,81002351267544641972,81002351235219878637,81002351267539940927,81002351267550400288,81002351235231397557,81002351267541381779,81002351267553281027,81002351236282589603,81002351267543204515,81002351267554696713,81002351236284027468,81002351267551841265,81002351267557574870,81002351236289782168,81002351267554121093,81002351267559014313,81002351236292661806,81002351267557003272,81002351267561327829,81002351236296984186,81002351267558444523,81002351267562715418,81002351236299860722,81002351267559883506,81002351267563334635,81002351267502845276,81002351267564200352,81002351267566214591,81002351267505722256,81002351267564770523,81002351267567080514,81002351267533124331,81002351267565644588,81002351267576513615,81002351267534187595,81002351267567650224,81002351267577556077,81002351267534561859,81002351267569098030,81002351267580431625,81002351267537065940,81002351267569913230,81002351267581483524,81002351267537449033,81002351267569962948,81002351267582925573,81002351267538509122,81002351267570535010,81002351267583710431,81002351267540325452,81002351267573417777,81002351267585155868,81002351267542828352,81002351267574283662,81002351267586591650,81002351267546083904,81002351267575728359,81002351267588682689,81002351267547520980,81002351267576111300,81002351267589473916,81002351267548969096,81002351267578603658,81002351267590122855,81002351267556130966,81002351267579391346,81002351267590913694,81002351267560452762,81002351267580045638,81002351267592356206,81002351267561276823,81002351267580839612,81002351267593005340,81002351267561892465,81002351267585806366,81002351267594440483,81002351267562765680,81002351267591005412,81002351267598432225,81002351267564158048,81002351267592448613,81002351267601312675,81002351267565596779,81002351267593882286,81002351267604443259,81002351267567039567,81002351267595238323,81002351267605632883,81002351267568475907,81002351267595321498,81002351267605886361,81002351267568521068,81002351267597327035,81002351267607320933,81002351267571405341,81002351267598111028,81002351267609952926,81002351267571976398,81002351267599600599,81002351267611396395,81002351267572840343,81002351267599878236,81002351267614275361,81002351267574855405,81002351267600863523,81002351267615712601,81002351267577165363,81002351267601048383,81002351267617326992,81002351267577951120,81002351267602480673,81002351267618149903,81002351267578996102,81002351267607074085,81002351267618590718,81002351267582270790,81002351267617151547,81002351267621094107,81002351267584360373,81002351267620031914,81002351267621880635,81002351267588038442,81002351267620201873,81002351267623979768,81002351267591563612,81002351267620444485,81002351267627085326,81002351267593795734,81002351267622533890,81002351267628294314,81002351267595884927,81002351267622700999,81002351267629082591,81002351267596676995,81002351267624146171,81002351267629906333,81002351267596760065,81002351267624204010,81002351267631965114,81002351267599424080,81002351267625581266,81002351267632783145,81002351267602754386,81002351267625644835,81002351267635498293,81002351267603005294,81002351267626202578,81002351267637100891,81002351267604198102,81002351267627020934,81002351267638372199,81002351267608515831,81002351267628462293,81002351267638540702,81002351267608762996,81002351267629738221,81002351267639168816,81002351267612832841,81002351267634050175,81002351267639816816,81002351267615880531,81002351267636931677,81002351267639922055,81002351267616705653,81002351267637725755,81002351267644542846,81002351267618760058,81002351267640609357,81002351267648865352,81002351267619584798,81002351267644371545,81002351267650302344,81002351267621261299,81002351267645983766,81002351267653803747,81002351267623321625,81002351267650920418,81002351267656684210,81002351267624761235,81002351267651747155,81002351267658124269,81002351267625418188,81002351267653180033,81002351267658941329,81002351267626857654,81002351267654623937,81002351267663883603,81002351267627640324,81002351267657505387,81002351267666211083,81002351267630525116,81002351267659569945,81002351267667652588,81002351267631176623,81002351267661002809,81002351267672093354,81002351267631342652,81002351267664705707,81002351267673111165,81002351267632612655,81002351267669093971,81002351267674976378,81002351267633407136,81002351267673531283,81002351267676415347,81002351267634167940,81002351267674557002,81002351267679299221,81002351267634221192,81002351267677851938,81002351267682176276,81002351267634842477,81002351267683615630,81002351267684632968,81002351267635660580,81002351267685053539,81002351267686075927,81002351267636287841,81002351269963106769,81002351269964542664,81002351267637045487,81002351269968860773,81002351269970302897,81002351267638486825,81002351269971740102,81002351270074051374,81002351267639987040,81002351269974623754,81002351270078370773,81002351267641252828,81002351270075493661,81002351271765460852,81002351267641362752,81002351270076931349,81002351271769788911,81002351267641429408,81002351272936845192,81002351272032060748,81002351267642047639,81002351272948362717,81002351272939724369,81002351267642931901,81002351272959881673,81002351272941161271,81002351267643103945,81002351272996064631,81002351272942607412,81002351267647420124,81002351273011902902,81002351272946922472,81002351267649485416,81002351273081012665,81002351272949804393,81002351267652361541,81002351273870532778,81002351272951243173,81002351267656064130,81002351273873414119,81002351272987424167,81002351267660384320,81002351273885520012,81002351272988863066,81002351267661822738,81002351273886475279,81002351272990300241,81002351267662445024,81002351273886962655,81002351272993182144,81002351267663264327,81002351272997505986,81002351267680736411,81002351273001823333,81002351269965988241,81002351273003262935,81002351269967421707,81002351273004701249,81002351269973185842,81002351273006140980,81002351271766901301,81002351273014785725,81002351271768345333,81002351273082458315,81002351271967009506,81002351273866215045,81002351272938287515,81002351273869090581,81002351272944045915,81002351273881201329,81002351272945484202,81002351273882643003,81002351272985986058,81002351273883598278,81002351272991746042,81002351273884084840,81002351272994620286,81002351273885031455,81002351272998943863,81002351273000381546,81002351273007582955,81002351273013346989,81002351273083891140,81002351273867652169,81002351273871978800,81002351273879762766,81002351273880713933,81002351273882154257";
//        String path = "C:\\Users\\Administrator\\Desktop\\code\\";
//        String[] s = msg.split(",");
//        for(int i =0 ; i < s.length;i++){
//            generateFile(s[i], path+i+".png");
//        }
//        generateFile("81002351235123051293", "C:\\Users\\Administrator\\Desktop\\code\\a.png");

    }

}